<template>
  <div class='xtx-carousel' @mouseenter="stop" @mouseleave="start">
    <!-- 轮播图图片列表 -->
    <ul class="carousel-body">
      <li class="carousel-item" :class='{fade: index===i}' v-for='(item,i) in sliders' :key='item.id'>
        <!-- 单张图的情况 -->
        <RouterLink to="/" v-if='item.imgUrl'>
          <img :src="item.imgUrl" alt="">
        </RouterLink>
        <!-- 4张图的情况 -->
        <div class="slider" v-else>
          <RouterLink v-for="goods in item" :key="goods.id" :to="`/product/${goods.id}`">
            <img :src="goods.picture" alt="">
            <p class="name ellipsis">{{goods.name}}</p>
            <p class="price">&yen;{{goods.price}}</p>
          </RouterLink>
        </div>
      </li>
    </ul>
    <!-- 左右两侧的点击按钮 -->
    <a @click='toggle(-1)' href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click='toggle(1)' href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <!-- 底部的指示器 -->
    <div class="carousel-indicator">
      <span @click='index=i' :class='{active: index===i}' v-for="(item, i) in sliders" :key="item.id"></span>
    </div>
  </div>
</template>

<script>
import { onUnmounted, ref, watch } from 'vue'

export default {
  name: 'XtxCarousel',
  props: {
    sliders: {
      type: Array,
      default: () => []
    },
    autoplay: {
      type: Boolean,
      default: false
    },
    duration: {
      type: Number,
      default: 1
    }
  },
  setup (props) {
    // 记录当前选中的项的索引
    const index = ref(0)
    const timer = ref(null)

    // 封装一个函数，实现自动轮播
    const autolayFn = () => {
      // 保证仅仅有一个定时任务
      timer.value && clearInterval(timer.value)
      timer.value = setInterval(() => {
        index.value++
        if (index.value >= props.sliders.length) {
          // 如果轮播图过了最后一张，那么从新回到第一张即可
          index.value = 0
        }
      }, props.duration * 1000)
    }

    watch(() => props.sliders, (newValue) => {
      // 如果轮播图数据发生变化，就触发自动轮播
      index.value = 0
      if (props.autoplay && newValue.length > 1) {
        // 自动轮播标志位必须成立，并且轮播图数量大于1
        autolayFn()
      }
    }, {
      immediate: true
    })

    // 控制自动轮播的终止
    const stop = () => {
      // 关闭定时器
      if (timer.value) {
        clearInterval(timer.value)
      }
    }

    // 控制自动轮播的开始
    const start = () => {
      if (props.autoplay && props.sliders.length > 1) {
        // 自动轮播标志位必须成立，并且轮播图数量大于1
        autolayFn()
      }
    }

    // 控制轮播图手动切换(上一页和下一页)
    const toggle = (i) => {
      index.value += i
      if (index.value < 0) {
        // 如果切换到最左侧，再点击时，移动到最后
        index.value = props.sliders.length - 1
      } else if (index.value >= props.sliders.length) {
        // 如果切换到了最后一张，再点击时，回到第一张
        index.value = 0
      }
    }

    // 组件销毁时，清除定时器
    onUnmounted(() => {
      stop()
    })

    return { index, stop, start, toggle }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      .slider {
        display: flex;
        justify-content: space-around;
        padding: 0 40px;
        > a {
          width: 240px;
          text-align: center;
          img {
            padding: 20px;
            width: 230px !important;
            height: 230px !important;
          }
          .name {
            font-size: 16px;
            color: #666;
            padding: 0 40px;
          }
          .price {
            font-size: 16px;
            color: @priceColor;
            margin-top: 15px;
          }
        }
      }
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
